package com.example.administrator.mymusic;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.example.administrator.mymusic.Class.SongsClass;

import java.io.IOException;
import java.util.List;


public class MyService extends Service {
    private MyBinder myBinder = new MyBinder();
    MediaPlayer mPlayerService;
    private List<SongsClass> songsClassList;
    private SongsClass songsClass;
    private int songNum = 0;
    public int maxSeekBar = 10000;
    private boolean isStart = false;
    public boolean isRuning = false;
    private boolean isTouch = false;
    private boolean Run = true;

    @Override
    public void onCreate() {
        mPlayerService = new MediaPlayer();
        playOver();
        Log.d("33333", "onCreate: " + startId);
        new Thread() {
            public void run() {
                Bundle bundle = new Bundle();
                while (Run) {
                    if (isStart) {
                        if (!isTouch) {
                            bundle.putInt("seekbar", mPlayerService.getCurrentPosition());
                            bundle.putSerializable("songs", songsClass);
                            if (cillBack != null) {
                                cillBack.onDataChange(bundle);
                            }
                        }
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }.start();
    }

    public void Seekbar(boolean isTouch, int seek) {
        this.isTouch = isTouch;
        mPlayerService.seekTo(seek);
        Log.d("33333", "Seekbar: " + startId);
    }

    public interface CillBack {
        void onDataChange(Bundle bundle);
    }

    private CillBack cillBack = null;

    public void setCillBack(CillBack cillBack) {
        this.cillBack = cillBack;
    }

    int startId = 0;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("33333", "onStartCommand: " + startId + "kkkkk" + flags);
        this.startId = startId;
        songNum = intent.getIntExtra("song", 0);
        paly();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        songsClassList = (List<SongsClass>) intent.getSerializableExtra("songs");
        return myBinder;
    }

    public class MyBinder extends Binder {
        public MyService getMyService() {
            return MyService.this;
        }
    }

    private static final String ACTION_LRC = "android.intent.action.LRC";

    /**
     * 播放
     */
    public void paly() {
        songsClass = songsClassList.get(songNum);
        mPlayerService.reset();
        try {
            mPlayerService.setDataSource(songsClass.getPath());
            mPlayerService.prepare();
            mPlayerService.start();
            maxSeekBar = mPlayerService.getDuration();
            isStart = true;
            isRuning = true;
            Intent intent = new Intent();
            intent.setAction(ACTION_LRC);
            intent.putExtra("songNum", songNum);
            MyService.this.sendBroadcast(intent);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 暂停_继续
     */
    public void start_pause() {
        if (isStart) {
            if (mPlayerService.isPlaying()) {
                mPlayerService.pause();
                isRuning = false;
            } else {
                mPlayerService.start();
                isRuning = true;
            }
        } else {
            paly();
        }
    }

    /**
     * 下一首
     */
    public void go() {
        if (songNum + 1 == songsClassList.size()) {
            songNum = 0;
        } else
            songNum += 1;
        paly();
    }

    /**
     * 上一首
     */
    public void back() {
        if (songNum == 0) {
            songNum = songsClassList.size() - 1;
        } else
            songNum += -1;
        paly();

    }

    public void playOver() {
        mPlayerService.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {

                switch (MainActivity.sXunhuan % 3) {
                    case 0:
                        go();
                        break;
                    case 1:
                        paly();
                        break;
                    case 2:
                        songNum = (int) (Math.random() * (songsClassList.size() - 1));
                        paly();
                        break;
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isStart = false;
        Run = false;

    }

}
